<?php
	if (!isset($_POST['name']) || $_POST['name'] == '') {
		$Msg_ERROR = 'Preencha o campo Nome';
	} else {
		$VrfName = str_replace(' ','',$_POST['name']);
	$GET = $GLOBALS['MYT']->db_array("SELECT ccenter_providers.ref, CODE FROM systemoffice.ccenter_providers 
				LEFT OUTER JOIN systemoffice.ccenter_providerscodes ON ccenter_providers.ref = RefProvider
			WHERE name = REPLACE('{$VrfName}',' ','') AND CODE = {$_POST['CODE']}");
	if (!isset($GET[1]['ref'])) {
	$GLOBALS['MYT']->db_query("INSERT INTO systemoffice.ccenter_providers (name, TYPE, Status, NIF) VALUES ('{$_POST['name']}',{$_POST['TYPE']},1,0)");
	$GET = $GLOBALS['MYT']->db_array("SELECT ref FROM systemoffice.ccenter_providers
			WHERE REPLACE(name,' ','') = REPLACE('{$_POST['name']}',' ','') AND TYPE = {$_POST['TYPE']}	ORDER BY Ref DESC LIMIT 0,1");
	}
	if (!isset($GET[1]['CODE'])) {
	$GLOBALS['MYT']->db_query("INSERT INTO systemoffice.ccenter_providerscodes (RefProvider, CODE) VALUES ({$GET[1]['ref']},{$_POST['CODE']})");
	}
	}
	if (isset($_POST['EditProvider'])) {
	if (!isset($_POST['name']) || $_POST['name'] == '') {
		$Msg_ERROR = 'Preencha o campo Designa��o';
	} else {
	$GLOBALS['MYT']->db_query("UPDATE systemoffice.ccenter_providers
						SET
							name = '{$_POST['name']}',
							TYPE = {$_POST['TYPE']},
							Status = {$_POST['Status']},
							Observations = '{$_POST['Observations']}',
							NIF = '{$_POST['Tax']}'
						WHERE 
							Ref = {$_POST['Provider']}");
		}
	}
	if (isset($_POST['AddProviderCode'])) {
	$GLOBALS['MYT']->db_query("INSERT INTO systemoffice.ccenter_providerscodes
			(RefProvider, CODE)
			VALUES
			({$GET[1]['Ref']},{$_POST['CODE']})");
	}
	if (isset($_POST['RemProvider'])) {
	$GLOBALS['MYT']->db_query("UPDATE systemoffice.ccenter_providers
			SET Status = 5
			WHERE Ref = {$_POST['Provider']}");
	}
	if (isset($_POST['RemProviderCode'])) {
	$GLOBALS['MYT']->db_query("UPDATE systemoffice.ccenter_providerscodes
			SET Status = 5
			WHERE Ref = {$_POST['ReProvider']}");
	}

	if (isset($Msg_ERROR)) {
		echo '<div class="Error">'.$Msg_ERROR.'</div>';
	} else {
		echo '<div class="Confirm">Concluido.</div>
		<script charset="utf-8" type="text/javascript">
		document.edtCdcInvoice.action=\'Modules/CentrodeCustos/Lib/sel_provider.php?id='.$GET[1]['ref'].'\';
		subform(\'edtCdcInvoice\',\'ProviderDiv\');
		Ext.getCmp(\'winprop\').close();</script>';
	}
?>